package view;

import java.awt.BorderLayout;
import java.awt.Scrollbar;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JPanel;

/**
 * 06-01-2010
 * 
 * Main panel which loads the appropiate title and content view of current CalendarMode.
 * 
 * @author Dave Nieuwenhuijzen 
 */
public class CalendarView extends JPanel implements ComponentListener {
	private static final long serialVersionUID = 1L;
	private model.CalendarModel calendarModel;
	private CalendarContentView calendarContentView;
	private Scrollbar scrollbar;
	
	/**
	 * This method initializes the components
	 */
	public CalendarView(model.CalendarModel setCalendarModel, model.AppointmentModel appointmentModel, controller.AppointmentController setAppointmentController) {		
		calendarModel = setCalendarModel;

		addComponentListener(this);

		CalendarTitleView calendarTitleView = new CalendarTitleView(calendarModel);
		calendarContentView = new CalendarContentView(calendarModel, appointmentModel);

		scrollbar = new Scrollbar(Scrollbar.VERTICAL);
		scrollbar.addAdjustmentListener(new controller.CalendarScrollbarController(calendarModel));

		calendarModel.setScrollbar(scrollbar);

		calendarModel.addObserver(calendarTitleView);
		calendarModel.addObserver(calendarContentView);
		appointmentModel.addObserver(calendarContentView);

		calendarModel.setCalendarViewMode(model.CalendarViewMode.week);

		calendarContentView.addMouseListener(new controller.CalendarContentController(calendarModel, appointmentModel, setAppointmentController));
		calendarContentView.addMouseWheelListener(new controller.CalendarScrollbarController(calendarModel));

		setLayout(new BorderLayout());
		add(calendarTitleView, BorderLayout.NORTH);
		add(calendarContentView, BorderLayout.CENTER);
		add(scrollbar, BorderLayout.EAST);
	}

	/**
	 * Resizes the entire component.
	 */
	private void resize() {
		calendarContentView.resetBounds();

		calendarModel.getScrollbar().setVisible(calendarModel.getCalendarViewMode() == model.CalendarViewMode.week);
		if (calendarModel.getCalendarViewMode() == model.CalendarViewMode.week) {
			// 12 is the hours are showed on the week view
			if(calendarModel.getScrollMax() != ((calendarContentView.getHeight() * 24) / 12)) {
				calendarModel.setScrollMax((int)((calendarContentView.getHeight() * 24) / 12));
			}
		} else {
			calendarModel.setScrollMax(calendarContentView.getHeight());
		}

		scrollbar.setMaximum(calendarModel.getScrollMax() - calendarContentView.getHeight());

		calendarModel.setScroll(scrollbar);
	}

	/**
	 * Overrided ComponentListener method that resizes the entire component.
	 */
	public void componentResized(ComponentEvent e) {
		resize();
	}

	/**
	 * Overrided ComponentListener methods, which is not used.
	 * 
	 * @param e
	 */
	public void componentHidden(ComponentEvent e) {

	}

	/**
	 * Overrided ComponentListener methods, which is not used.
	 * 
	 * @param e
	 */
	public void componentMoved(ComponentEvent e) {

	}

	/**
	 * Overrided ComponentListener methods, which is not used.
	 * 
	 * @param e
	 */
	public void componentShown(ComponentEvent e) {

	}
}